<?php
    @include_once("conn.php");
    @include_once("common.php");

    $key=$_GET["key"];
    $orderCol=$_GET["orderCol"];
    $orderType=$_GET["orderType"];
    $page=$_GET["page"];
    $selectValue=$_GET["selectValue"];
    
    
    if(!($orderCol&&$orderType)){
        $msg=array();
        $msg["status"]=false;
        $msg["detail"]="请输入完整参数";
        $msg["key"]=$key;
        $msg["orderCol"]=$orderCol;
        $msg["orderType"]=$orderType;
        exit(json_encode($msg));
    }
    $number="select count(*) as count from `study` where name like '%$key%'";
    $numberRst=mysqli_query($conn,$number);
    // $arr=array();
    $numCount=mysqli_fetch_assoc($numberRst);
    $count=$numCount["count"];
    
    $maxPage=ceil(($count*1)/($selectValue*1));
    if($page>=$maxPage){
        $page=$maxPage;
    }


    
    $pagenum=($page*1-1)*$selectValue;
    $rearch="select id,name,class,chinese,math,english,chinese+math+english as total from `study` where name like '%$key%' order by $orderCol $orderType limit $pagenum,$selectValue";
    $reslut=mysqli_query($conn,$rearch);

    

    $obj=array();
    $list=array();
    while($item=mysqli_fetch_assoc($reslut)){
        $item["chinese"] = $item["chinese"]*1;
        $item["math"] = $item["math"]*1;
        $item["english"] = $item["english"]*1;
        $item["total"] = $item["total"]*1;
        array_push($list,$item);
    }

    if(count($list)>0){
        $obj["status"]=true;
        $obj["detail"]="查询成功";
        $obj["list"]=$list;
        $obj["maxPage"]=$maxPage;
        $obj["page"]=$page;

    }else{
        $obj["status"]=false;
        $obj["detail"]="请输入正确参数";
        $obj["sql"]=$rearch;
    }
    echo json_encode($obj);



?>